/**
 *  轻学习表
 */
'use strict';

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var ProcessSchema = new Schema({
    author_id:{
        type: Schema.Types.ObjectId,
        ref: 'User'
    },
    author:{
        type: String,
        default:'user'
    },
    portrait: { // 封面图片
        type: String
    },
    title:{ // 标题
        type:String,
        unique: true
    },
    toolList:{ // 工具列表
        type:Array
    },
    processList:{ // 流程列表
        type:Array
    },
    //一篇文章可以有多个标签
    tags:[{
        type: Schema.Types.ObjectId,
        ref: 'Tag'
    }],
    visit_count:{			//访问数
        type:Number,
        default:1
    },
    comment_count:{		//评论数
        type:Number,
        default:0
    },
    like_count:{
        type:Number,
        default:1
    },
    top:{
        type:Boolean,
        default:false
    },
    status:{				//0:草稿 1:发布
        type:Number,
        default:0
    },
    created: {
        type: Date,
        default: Date.now
    },
    publish_time: {
        type: Date,
        default: Date.now
    },
    updated: {
        type: Date,
        default: Date.now
    }
});

ProcessSchema
    .virtual('overview')
    .get(function () {
       return {
           '_id': this._id,
           'author_id': this.author_id,
           'portrait': this.portrait,
           'author':this.author,
           'title': this.title,
           'cover': this.processList[0],
           'visit_count': this.visit_count,
           'comment_count':this.comment_count,
           'like_count':this.like_count,
           'publish_time': this.publish_time
       }
    });

ProcessSchema
    .virtual('info')
    .get(function() {
        return {
            '_id': this._id,
            'author_id': this.author_id,
            'portrait': this.portrait,
            'author':this.author,
            'title': this.title,
            'processList': this.processList,
            'toolList': this.toolList,
            'visit_count': this.visit_count,
            'comment_count':this.comment_count,
            'like_count':this.like_count,
            'publish_time': this.publish_time
        };
    });

var Process = mongoose.model('Process',ProcessSchema);

var Promise = require('bluebird');
Promise.promisifyAll(Process);
Promise.promisifyAll(Process.prototype);

module.exports = Process;